package com.biomatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biomatch.domain.ContainerLock;
import com.biomatch.domain.Group;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.ContainerLockPagination;
import com.biomatch.dto.Pagination;
import com.biomatch.mapper.ContainerLockMapper;
import com.biomatch.service.ContainerLockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ContainerLockServiceImpl implements ContainerLockService {

    @Autowired
    ContainerLockMapper containerLockMapper;

    @Override
    public ContainerLock saveOrUpdateContainerLock(ContainerLock containerLock) {
        String GroupId=containerLock.getGroup().getGroupId();
        String RuleId=containerLock.getRule().getRuleId();
        //String RoleId=containerLock.getRole().getRoleId();
        String SuperId=containerLock.getSuperUser().getUsrcod();
        //增加液氮罐锁
        if(containerLock.getId()<=0)
        {
            QueryWrapper<ContainerLock> containerLockQueryWrapper = new QueryWrapper<ContainerLock>();
            containerLockQueryWrapper.eq("containerLockName",containerLock.getContainerLockName());
            if(containerLockMapper.selectCount(containerLockQueryWrapper)>0)
            {
                return  null;
            }
            else
            {
                String ContainerLockId = String.valueOf(new Date().getTime());
                containerLock.setContainerLockId("CL"+ContainerLockId.substring(5,ContainerLockId.length()));
                containerLock.setCreateTime(new Date());
                //锁类型不用 锁id 关联三个表  前端 创建者、位置ID、液氮罐Id  groupid roleid
                containerLockMapper.insert(containerLock);
                containerLockMapper.insertLockAndGroupAndRule(containerLock.getContainerLockId(),GroupId,RuleId,SuperId);
            }
            return containerLock;
        }
        //更新液氮罐锁
        else{
            QueryWrapper<ContainerLock> containerLockQueryWrapper=new QueryWrapper<>();
            containerLockQueryWrapper.eq("containerLockName",containerLock.getContainerLockName()).ne("ContainerLockId",containerLock.getContainerLockId());
            if(containerLockMapper.selectCount(containerLockQueryWrapper)>0)
            {
                return  null;
            }
            else
            {
                containerLockMapper.updateLockAndGroupAndRule(containerLock.getContainerLockId(),GroupId,RuleId,SuperId);
                containerLockMapper.updateById(containerLock);
            }
            return containerLock;
        }
    }

    @Override
    public List<ContainerLock> getContainerLockList() {
        return containerLockMapper.selectList(null);
    }


    @Override
    public IPage<ContainerLock> getContainerLockAndGroupAndRule(ContainerLockPagination pagination) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("start",(pagination.getCurrent()-1) * pagination.getPageSize());
        params.put("pageSize",pagination.getPageSize());
        params.put("keywords",pagination.getKeywords());
        params.put("positionId",pagination.getPositionId());
        //获取总记录数据
        pagination.setTotal(containerLockMapper.findCountByMap(params));

        IPage<ContainerLock> iPage = new Page<>(pagination.getCurrent(), pagination.getPageSize(),pagination.getTotal());
        iPage.setRecords(containerLockMapper.getcontainerLockByPage(params));

        //Page<ContainerLock> page = new Page<>(pagination.getCurrent(), pagination.getPageSize());
       // IPage<ContainerLock> iPage = containerLockMapper.getcontainerLockByPage(page);
        return iPage;
    }

    @Override
    public ContainerLock delContainerLockById(ContainerLock containerLock) {
        String containerLockId = containerLock.getContainerLockId();
        UpdateWrapper<ContainerLock> containerLockUpdateWrapper = new UpdateWrapper<>();
        containerLockUpdateWrapper.eq("containerLockId", containerLock.getContainerLockId());
        if(containerLockId!=null)
        {
            containerLockMapper.delete(containerLockUpdateWrapper);
            containerLockMapper.deleteLockAndRuleAndGroup(containerLockId);
        }
        return containerLock;
    }
}
